//
//  RequestManager.m
//  Leisure
//
//  Created by xalo on 16/6/28.
//  Copyright © 2016年 朱旭东. All rights reserved.
//

#import "RequestManager.h"

@implementation RequestManager

//请求数据，根据类型会自动识别请求方式
+(void)fetchDataWithURL:(NSString *)url parameters:(NSDictionary *)dic method:(NSString *)method  complet:(RequestCompleted)complete  updataUI:(UpdataUI)updataUI{
    
    //创建请求对象，并设置请求对象的属性
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
   //创建一个子线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        if([method isEqualToString:@"GET"]){
            [manager GET:url parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                //把data数据传出去
                complete((NSData*)responseObject);
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    //刷新UI
                    updataUI();
                });

            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
        }else{
            [manager  POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
                
            } progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                //把data数据传出去
                complete((NSData*)responseObject);
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    //刷新UI
                    updataUI();
                });
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }
        ];}
    });
    

}
@end
